package com.justonetech.translation.manager;

import com.justonetech.system.domain.SysDept;
import com.justonetech.system.domain.SysPerson;
import com.justonetech.translation.domain.Contract;
import com.justonetech.translation.domain.Project;
import com.lowagie.text.*;
import com.lowagie.text.Font;
import com.lowagie.text.Rectangle;
import com.lowagie.text.pdf.BaseFont;
import com.lowagie.text.rtf.RtfWriter2;
import org.springframework.stereotype.Service;

import java.awt.*;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
 * Created by IntelliJ IDEA.
 * User: tcg
 * Date: 12-7-14
 * Time: 下午10:21
 * To change this template use File | Settings | File Templates.
 */
@Service
public class ContractManager {
    private String _BLANK = " ";
    private String UNIT = "(元)";
    public void createDocContext(OutputStream fop,Contract contract,SysPerson person)throws DocumentException, IOException {
        Project project = contract.getProject();
        SysDept sysDept = contract.getFirstParty();
        //设置纸张大小
        Document document = new Document(PageSize.A4);
        document.setMargins(60,60,60,60);
        //建立一个书写器，与document对象关联
        RtfWriter2.getInstance(document,fop);
        document.open();
        //设置中文字体
        BaseFont bfChinese = BaseFont.createFont("STSongStd-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
        //标题字体风格
        Font titleFont = new Font(bfChinese,10,Font.BOLD);
        //正文字体风格
        Font contextFont = new Font(bfChinese,10,Font.NORMAL);
        Paragraph title = new Paragraph("翻译委托书");
        //设置标题格式对齐方式
        title.setAlignment(Element.ALIGN_CENTER);
        title.setFont(titleFont);
        document.add(title);
        Paragraph context = new Paragraph();
        context.add("兹有");
        context.add(new Chunk(_BLANK+ contract.getFirstParty().getName()+_BLANK,FontFactory.getFont(FontFactory.HELVETICA,10,Font.UNDERLINE)));
        context.add("委托上海琢言文化传播有限公司");
        context.add(new Chunk(_BLANK+contract.getProject().getName()+_BLANK,FontFactory.getFont(FontFactory.HELVETICA,10,Font.UNDERLINE)));
        context.add("方面的资料。");
        context.setAlignment(Element.ALIGN_LEFT);
        context.setFont(contextFont);
        //段间距
//        context.setSpacingBefore(0);
        //设置第一行空的列数
        context.setFirstLineIndent(0);

        document.add(context);
        //设置Table表格,创建一个三列的表格
        Table table = new Table(6);
        int width[] = {10,20,12,20,10,25};//设置每列宽度比例
        table.setWidths(width);
        table.setWidth(99);//占页面宽度比例
        table.setAlignment(Element.ALIGN_CENTER);//居中
        table.setAlignment(Element.ALIGN_MIDDLE);//垂直居中
//        table.setAutoFillEmptyCells(true);//自动填满
        table.setBorderWidth(0);//边框宽度
        table.setPadding(8);
        table.setBorder(1);
        //设置表头

        Cell haderCell = new Cell(new Chunk("客户信息",FontFactory.getFont(FontFactory.HELVETICA,10)));
        haderCell.setHeader(true);
        haderCell.setColspan(6);
        table.addCell(haderCell);
        table.endHeaders();

        Cell cell = new Cell(new Paragraph("客户名称",contextFont));
        cell.setVerticalAlignment(Element.ALIGN_MIDDLE);
        table.addCell(cell);
        table.addCell(new Cell(sysDept.getName()));
        Cell cell01 = new Cell(new Paragraph("联系人", contextFont));
        table.addCell(cell01);
        table.addCell(new Cell(new Paragraph(sysDept.getLinker(),contextFont)));
        Cell cell02 = new Cell(new Paragraph("电话", contextFont));
        table.addCell(cell02);
        table.addCell(new Cell(new Paragraph(sysDept.getTelephone(),contextFont)));

        table.addCell(new Cell(new Paragraph("E-mail", contextFont)));
        table.addCell(new Cell(new Paragraph(sysDept.getEmail(),contextFont)));
        table.addCell(new Cell(new Paragraph("网址", contextFont)));
        table.addCell(new Cell(new Paragraph(sysDept.getNetSite(),contextFont)));
        table.addCell(new Cell(new Paragraph("传真", contextFont)));
        table.addCell(new Cell(new Paragraph(sysDept.getFax(),contextFont)));

        Cell hader2Cell = new Cell(new Chunk("翻译项目信息",FontFactory.getFont(FontFactory.HELVETICA,10)));
        hader2Cell.setHeader(true);
        hader2Cell.setColspan(6);
        table.addCell(hader2Cell);

        table.addCell(new Cell(new Paragraph("项目编号", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getCode(),contextFont)));
        table.addCell(new Cell(new Paragraph("原稿名称", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getSourceName(),contextFont)));
        table.addCell(new Cell(new Paragraph("开始时间", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getStartDate().toString(),contextFont)));

        table.addCell(new Cell(new Paragraph("源语言", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getSouceLanguage().getName(),contextFont)));
        table.addCell(new Cell(new Paragraph("目标语言", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getTargetLanguage().getName(),contextFont)));
        table.addCell(new Cell(new Paragraph("结束时间", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getEndDate().toString(),contextFont)));

        table.addCell(new Cell(new Paragraph("项目类型", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getType().getName(),contextFont)));
        table.addCell(new Cell(new Paragraph("字数", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getWordCount().toString(),contextFont)));
        table.addCell(new Cell(new Paragraph("要求", contextFont)));
        table.addCell(new Cell(new Paragraph(project.getRequirement(),contextFont)));

        Cell hader3Cell = new Cell(new Chunk("合同信息",FontFactory.getFont(FontFactory.HELVETICA,10)));
        hader3Cell.setHeader(true);
        hader3Cell.setColspan(6);
        hader3Cell.setTop(10f);
        table.addCell(hader3Cell);

        table.addCell(new Cell(new Paragraph("翻译价格", contextFont)));
        table.addCell(new Cell(new Paragraph(contract.getPrice()+"/千字符",contextFont)));
        table.addCell(new Cell(new Paragraph("专业技术费", contextFont)));
        table.addCell(new Cell(new Paragraph(contract.getProfessionalFee().toString()+UNIT,contextFont)));
        table.addCell(new Cell(new Paragraph("加急费", contextFont)));
        table.addCell(new Cell(new Paragraph(contract.getUrgentFee().toString()+UNIT,contextFont)));

        table.addCell(new Cell(new Paragraph("排版费", contextFont)));
        table.addCell(new Cell(new Paragraph(contract.getTypesettingFee().toString()+UNIT,contextFont)));
        table.addCell(new Cell(new Paragraph("总计金额", contextFont)));
        table.addCell(new Cell(new Paragraph(contract.getMoneyAmount().toString()+UNIT,contextFont)));
        table.addCell(new Cell(new Paragraph("预付款", contextFont)));
        table.addCell(new Cell(new Paragraph(contract.getAdvancePayment().toString()+UNIT,contextFont)));

        document.add(table);

        document.add(new Paragraph());
        Paragraph attention = new Paragraph("注:");
        //设置标题格式对齐方式
//        attention.setAlignment(Element.L);
        attention.setFont(contextFont);
        document.add(attention);

        Paragraph attention1 = new Paragraph("1. 预付款为50%，在双方签订委托书后3日内付清给被委托方。");
        //设置标题格式对齐方式
//        attention.setAlignment(Element.L);
        attention1.setFont(contextFont);
        attention1.setIndentationRight(4f);
        document.add(attention1);

        Paragraph attention2 = new Paragraph("2. 如果我方的翻译稿件中出现数字、地名、拼写错误及疏漏，我方负担重新修改责任；在译稿的风格方面，在有试译的前提下，将保持并超过试译风格。在无试译的情况下，客户须提前提供翻译背景、相关术语、及客户认可的翻译样本。");
        //设置标题格式对齐方式
        attention2.setFont(contextFont);
        attention2.setIndentationRight(4f);
        document.add(attention2);

        Paragraph attention3 = new Paragraph("3. 字数统计：中（日、韩）译外及外中（日、韩）稿件以计算机统计的“中（日、韩）文字符不计空格”为标准。其它外译外以原稿字数为标准。");
        //设置标题格式对齐方式
        attention3.setFont(contextFont);
        attention3.setIndentationRight(4f);
        document.add(attention3);

        Paragraph attention4 = new Paragraph("4. 付款方式: 请以银行转帐支票或现金方式在译文完整提交后15日内将款付至上海琢言文化传播有限公司。任何延误付费，本公司将每日加收总价1%的滞纳金。");
        //设置标题格式对齐方式
        attention4.setFont(contextFont);
        attention4.setIndentationRight(4f);
        document.add(attention4);

        document.add(new Paragraph());
        document.add(new Paragraph());
        document.add(new Paragraph("账号信息:"));

        Paragraph paragraph = new Paragraph("开户行：中国邮政储蓄银行有限责任公司上海宝山区支行           账  号：100219145170010001");
        paragraph.setFont(titleFont) ;
        document.add(paragraph);
        Paragraph paragraph1 = new Paragraph("户  名：上海琢言文化传播有限公司");
        paragraph1.setFont(titleFont);
        document.add(paragraph1);

        Paragraph paragraph2 = new Paragraph("委托方：                                                    被委托方：                ");
        paragraph2.setFont(titleFont);
        document.add(paragraph2);

        Paragraph paragraph3 = new Paragraph("（盖章）                                         上海琢言文化传播有限公司　\n" +
                "　年  月   日             　　　                                年  月  日");
        paragraph3.setFont(contextFont);
        document.add(paragraph3);

        HeaderFooter headerFooter = new HeaderFooter(new Phrase("联系电话：15021330242  021-61552160\n" +
                "传真号码：021-50588280\n" +
                "通信地址:上海东方路15号锦城大厦7B     邮编: 200122                            \n" +
                "网   址： www.splendidcommunication.com",new Font(bfChinese,10,Font.ITALIC)),true);
        headerFooter.setBorder( Rectangle.TOP);
        headerFooter.setUseVariableBorders(true);
        document.setFooter(headerFooter);
        document.close();
    }
}
